package com.yc.other.mail;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;


import javax.activation.*;
 
public class SendEmail
{
   public void send(String filepath)
   {   
	   String to = "yunduanyou@126.com";
	   String from = "13758238177@189.cn";
	   String userName = from;
	   String password = "1234qwer";
	   

      String host = "smtp.189.cn";
      
      //发件箱是否是exchange
      boolean isExchange = false;
      String domain = "360ser.com";
      
      //是否有附件
      boolean haveFile = true;
//      String filename = "F:\\worksource\\JKCT\\MAIL\\测试文件.pdf";
      String filename = filepath;
      File sendFile = new File(filename);
 
      // 获取系统属性
      Properties properties = System.getProperties();
 
      // 设置邮件服务器
      properties.setProperty("mail.transport.protocol", "SMTP");
      properties.setProperty("mail.smtp.host", host);
      properties.setProperty("mail.smtp.port", "25");
 
      // 指定验证为true
      properties.setProperty("mail.smtp.auth", "true");
      properties.setProperty("mail.smtp.timeout","1000");
      
      if(isExchange){
          if(domain == null || domain.equals("")){
              throw new RuntimeException("domain is null");
          }
          properties.setProperty("mail.smtp.auth.ntlm.domain", domain);
      }
      // 验证账号及密码，密码需要是第三方授权码
      MailAuthenticator smtpAuth = new MailAuthenticator(userName, password);
      Session session = Session.getInstance(properties, smtpAuth);
      session.setDebug(true);
      try{
         // 创建默认的 MimeMessage 对象
         MimeMessage message = new MimeMessage(session);
 
         // Set From: 头部头字段
         message.setFrom(new InternetAddress(from));
 
         // Set To: 头部头字段
         message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
         message.addRecipient(Message.RecipientType.CC, new InternetAddress(userName));
 
         // Set Subject: 头部头字段
         message.setSubject("测试文件2432421313213");
         // 设置消息体
//         message.setText("你好setText！1232142143214213");
         
         
         MimeMultipart mapAttach = new MimeMultipart("mixed");
         try {
				// 设置邮件正文
				MimeBodyPart testMimeBodyPart = new MimeBodyPart();
				testMimeBodyPart.setContent("你好setText！1232142143214213", "text/html;charset=utf-8");
				mapAttach.addBodyPart(testMimeBodyPart);
				
				message.setSentDate(new Date());
				message.setContent(mapAttach);
				message.saveChanges();
			} catch (MessagingException e) {
				e.printStackTrace();
			}
         
         
        
         // 发送消息
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
   
   public static void main(String[] args) {
	   SendEmail se = new SendEmail();
	   se.send(args[0]);
   }
}
